package com.da.javatest.leetCode;

import java.util.Arrays;

/**
 * @author chenlida
 * @date 2019/3/7 17:30
 * @description
 */
public class Code581 {
    public static void main(String[] args) {
        System.out.println(new Code581().findUnsortedSubarray(new int[] {2, 6, 4, 8, 10, 9, 15}));
    }

    public int findUnsortedSubarray(int[] nums) {
        int[] n = Arrays.stream(nums).sorted().toArray();
        for (int i = 0, j = nums.length - 1; i < nums.length; ) {
            if (n[i] != nums[i] && n[j] != nums[j]) {
                return j - i + 1;
            }
            if (n[i] == nums[i]) {
                i++;
            }
            if (n[j] == nums[j]) {
                j--;
            }
        }
        return 0;
    }
}
